package com.woniu.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.util.SaResult;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@ControllerAdvice
public class GlobalExceptionHandler {
  static Integer a=0;
    @ResponseBody
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public String handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        return "上传文件大小超过限制，请上传小于500MB的文件";
    }

//     全局异常拦截
    @ResponseBody
    @ExceptionHandler
    public SaResult handlerException(Exception e) {
        e.printStackTrace();
        return SaResult.error(e.getMessage());
    }

    // 处理未登录异常
    @ResponseBody
    @ExceptionHandler(NotLoginException.class)
    public SaResult handleNotLoginException(NotLoginException e) {
        // 根据不同的未登录类型返回不同的结果
        if (e.getType().equals(NotLoginException.NOT_TOKEN)) {
            return SaResult.error("未提供 Token").setCode(401);
        } else if (e.getType().equals(NotLoginException.INVALID_TOKEN)) {
            return SaResult.error("Token 无效").setCode(401);
        } else if (e.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
            return SaResult.error("Token 已过期").setCode(401);
        } else {
            return SaResult.error("未登录").setCode(401);
        }
    }
}
